package com.wd.jetpack.util;

import java.util.ArrayList;
import java.util.List;
import java.util.ResourceBundle;

public class MessageResourceBundles {
    private List<ResourceBundle> resourceBundles = new ArrayList<ResourceBundle>();

    public MessageResourceBundles(String[] baseNames) {
        if (baseNames != null && baseNames.length > 0) {
            for (String baseName : baseNames) {
                ResourceBundle res = ResourceBundle.getBundle(baseName);
                resourceBundles.add(res);
            }
        }
    }

    public String getMessage(String key) {
        String value = null;
        for (ResourceBundle resourceBundle : resourceBundles) {
            try {
                value = resourceBundle.getString(key);
            } catch (Exception e) {
                value = null;
            }

            if (value != null) {
                return value;
            }
        }

        return value;
    }

    public String getMessage(String key, String defaultValue) {
        String value = getMessage(key);
        if (value == null) {
            return defaultValue;
        } else {
            return value;
        }
    }
}
